/*
 * @(#)CMPPMsgHeader.java 1.0 2013-6-25
 *
 * Copyright (c) 2009 Sunyard System Engineering Co., Ltd. All rights reserved.
 *
 * This software is the confidential and proprietary information of Sunyard
 * System Engineering Co., Ltd. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with Sunyard.
 */
/*
 * Modification History: Date Author Version Description
 * ------------------------------------------------------------------
 *
 */

package vsmg.cmpp.entity;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.mina.core.buffer.IoBuffer;

import vsmg.util.ByteUtil;

/**
 *
 *
 * @version 1.0 2013-6-25
 * @author xinx.wang@sunyard.com
 */
public class CMPPMsgHeader {

	private static AtomicInteger	i			= new AtomicInteger(1);
	private int						totalLength;
	private int						commandId;
	private int						sequenceID	= 1;

	public CMPPMsgHeader() {
		super();
	}

	public CMPPMsgHeader(IoBuffer in, int pklen) throws Exception {
		this.setTotalLength(pklen);
		this.setCommandId(in.getInt());
		this.setSequenceID(in.getInt());
	}

	/**
	 * @return the totalLength
	 */
	public int getTotalLength() {
		return this.totalLength;
	}

	/**
	 * @return the commandId
	 */
	public int getCommandId() {
		return this.commandId;
	}

	/**
	 * @return the sequenceId
	 */
	public int genSequenceID() {
		int j = CMPPMsgHeader.i.getAndIncrement();
		if (j > 0x7fffffff) {
			CMPPMsgHeader.i.set(1);
			j = 1;
		}

		return j;
	}

	public int getSequenceID() {

		return this.sequenceID;
	}

	/**
	 * @param totalLength
	 *            the totalLength to set
	 */
	public void setTotalLength(int totalLength) {
		this.totalLength = totalLength;
	}

	/**
	 * @param commandId
	 *            the commandId to set
	 */
	public void setCommandId(int commandId) {
		this.commandId = commandId;
	}

	/**
	 * @param sequenceId
	 *            the sequenceId to set
	 */
	public void setSequenceID(int sequenceID) {
		if (sequenceID > 0x7fffffff) {
			sequenceID = 1;
		}
		this.sequenceID = sequenceID;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "[commandId=" + ByteUtil.number2HexStr(this.commandId) + ", sequenceID=" + ByteUtil.number2HexStr(this.sequenceID) + ", totalLength="
				+ ByteUtil.number2HexStr(this.totalLength) + "]";
	}

}
